$(document).ready(function() {

    //placeholder

//    jQuery('input[placeholder], textarea[placeholder]').placeholder();

    /*===========MENU animation==================*/
    $('.header ul li a').hover(
        function(){
            $(this).stop(true,true).animate({
                'margin-top': '-5px'
            });
        },
        function(){
            $(this).animate({
                'margin-top': '0px'
            });
        });

    /* ACTIVE MENU ITEM*/
//    $(function(){
//        $('.header ul li a').each(function(){
//            if(document.location.pathname === $(this).attr('href')){
//                var clasName= $(this).attr('class');
//                $(this).addClass(clasName+'_active');
//            }
//        });
//    });

    /*Contact AJAX */
    $('form[name=contact] input[type=submit]').click(function(e){

        e.preventDefault();
        var name = $(this).parent().parent().find('input[name=name]').val(),
            email =$(this).parent().parent().find('input[name=email]').val(),
            phone = $(this).parent().parent().find('input[name=phone]').val(),
            message = $(this).parent().parent().find('textarea').val();

        content = $('.contact_form_wrapper');
        loader(content.parent(), 'show');



        $.ajax({
            type    : 'POST',
            url     : '/contact/contact/',
            dataType: 'json',
            data    : {name:name,email:email,phone:phone,message:message},
            success : function(msg) {
//                $('.contact_form input[name=email]').val('');
//                $('.contact_form textarea').val('');
                alert('Thanks for your message! \n We will get back to you soon');
                window.location.href='/';
            },
            error   : function(msg){
                alert('Information is not valid.Please try again');
            },
            complete :  function(){
                loader(content.parent(), 'hide');
            }
        });
    })

    /*AJAX LOADER*/
    function loader($this, action) {
        var loader = new Image();
        loader.src = '/images/loader.gif';

        switch(action) {
            case 'show':
                if($this.children('.loader').length == 0) {
                    var wBlock = $(window).width();
                    var hBlock = $(window).height();
                    var c = '<div class="loader" style="position: fixed; display: none; opacity: 0.8"></div>';
                    $this.append(c);
                    var newLoader = new Image();
                    newLoader.src = loader.src;

                    $this.children('.loader').append(newLoader);
                    $this.children('.loader').css({
                        'top': (hBlock-newLoader.height > 0) ? (hBlock-newLoader.height)/2 : 0,
                        'left': (wBlock-newLoader.width > 0) ? (wBlock-newLoader.width)/2 : 0
                    }).fadeTo(100, 1);
                }
                break;
            case 'hide':
                $this.children('.loader').stop().fadeOut(100, function() {
                    $(this).remove();
                });
                break;
        }
    };
});